/**
 * @className CartController
 * @author liuxuelin
 * @Date 2021/1/19
 * @version v1.0
 */

package com.hidt.shop.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hidt.shop.domain.Announcement;
import com.hidt.shop.domain.Cart;
import com.hidt.shop.domain.CartAndGoods;
import com.hidt.shop.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.swing.text.StyledEditorKit;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
public class CartController {
    @Autowired
    private CartService cartService;


    /**
     *@方法功能描述：添加购物车的方法
     *@methodName addCartAll
     *@params [request]
     *@return java.lang.String
     *@author 刘雪林
     *@Date 2021/1/19
     */
    @RequestMapping(value = "/cart/add", method = {RequestMethod.POST})
    @ResponseBody
    public String addCartAll(@RequestBody Cart cart) {
//        Map<String, String[]> parameterMap = request.getParameterMap();
//        Set<String> stringSet = parameterMap.keySet();
//        Cart cart=null;
//        for (String  key:stringSet){
//            System.out.println(key);
//            ObjectMapper mapper=new ObjectMapper();
//            cart= mapper.readValue(key, Cart.class);
//            break;
//        }
//        int i = cartService.addCart(cart);
        int i = cartService.findAllCartByUserid(cart);
        if (i>0) {
            /*m.addAttribute("message","添加成功");*/
            return "添加成功1";
        }else{
            /*m.addAttribute("message","添加失败");*/
            return "添加失败0";
        }
    }
    /**
     *@方法功能描述：删除购物车信息
     *@methodName delteCartByid
     *@params [map]
     *@return java.lang.String
     *@author 刘雪林
     *@Date 2021/1/19
     */
    @RequestMapping(value = "/cart/del", method = {RequestMethod.POST})
    @ResponseBody
    public String delteCartByid(@RequestBody Map<String,String>  map){
        System.out.println(map.get("cart_id"));
        int i = cartService.deleteCartByid(Integer.parseInt(map.get("cart_id")));
        if (i>0) {
            /*m.addAttribute("message","添加成功");*/
            return "删除成功1";
        }else{
            /*m.addAttribute("message","添加失败");*/
            return "删除失败0";
        }
    }
    /**
     *@方法功能描述：修改购物车需要购买商品的数量
     *@methodName updateCartId
     *@params [request]
     *@return java.lang.String
     *@author 刘雪林
     *@Date 2021/1/19
     */
    @RequestMapping("/cart/update")
    @ResponseBody
    public String updateCartId(@RequestBody Cart cart) {
        int i = cartService.updateCartByid(cart);
        if (i>0) {
            /*m.addAttribute("message","添加成功");*/
            return "001";
        }else{
            /*m.addAttribute("message","添加失败");*/
            return "修改失败0";
        }
    }
    /**
     *@方法功能描述：查询购物车信息
     *@methodName delteCartByid
     *@params [map]
     *@return java.lang.String
     *@author 刘雪林
     *@Date 2021/1/19
     */
    @RequestMapping(value = "/cart/find", method = {RequestMethod.POST})
    @ResponseBody
    public List<CartAndGoods> findCartByUser_id(@RequestBody Map<String,String>  map) {
        System.out.println(map.get("user_id"));
        List<CartAndGoods> listcart = cartService.findAllCart(Integer.parseInt(map.get("user_id")));

        for(Object cart:listcart){
            System.out.println(cart);
        };
    return listcart;
    }
}
